package com.czy.owner.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.ShareEvent;
import com.czy.owner.utils.glide.config.GlideCacheConfig;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

import static com.czy.owner.global.Constants.GOODS_SHARE_CODE;

/**
 * Created by wangyang on 2016/10/8 0008.
 */
public class ShareUtils {
    /**
     * 演示调用ShareSDK执行分享
     *
     * @param context
     * @param platformToShare 指定直接分享平台名称（一旦设置了平台名称，则九宫格将不会显示）
     * @param showContentEdit 是否显示编辑页
     */

    public static void showShare(final Context context, final String platformToShare, boolean showContentEdit, final String shareUrl, final String title, final String content, String imgUrl, final String copy) {
        final OnekeyShare oks = new OnekeyShare();
        oks.setSilent(!showContentEdit);
        if (platformToShare != null) {
            oks.setPlatform(platformToShare);
            MyLog.e("yang", "platformToShare==" + platformToShare);
        }
        //ShareSDK快捷分享提供两个界面第一个是九宫格 CLASSIC  第二个是SKYBLUE
        oks.setTheme(OnekeyShareTheme.CLASSIC);
        // 令编辑页面显示为Dialog模式
        oks.setDialogMode();
        // 在自动授权时可以禁用SSO方式
        oks.disableSSOWhenAuthorize();


        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                MyLog.e("yang", "platform.getName()==" + platform.getName());
                if (SinaWeibo.NAME.equals(platform.getName())) {
                    paramsToShare.setText(title + shareUrl);
                    paramsToShare.setUrl(null);

                }
            }
        });


        if (content.equals("约惠双十一,养车狂欢节")) {
            oks.setTitle("约惠双十一,养车狂欢节");
        } else {
            oks.setTitle(title);
        }
        oks.setTitleUrl(shareUrl);
        oks.setText(content);
        if (TextUtils.isEmpty(imgUrl)) {
            if (content.equals("约惠双十一,养车狂欢节")) {
                imgUrl = Environment.getExternalStorageDirectory() + GlideCacheConfig.GLIDE_CARCH_DIR + "/owner_share_double11_icon.png";
            } else {
                imgUrl = Environment.getExternalStorageDirectory() + GlideCacheConfig.GLIDE_CARCH_DIR + "/owner_share_icon.png";
            }

            oks.setImagePath(imgUrl);
            MyLog.e("yang", "setImagePath=====" + imgUrl);
        } else {
            oks.setImageUrl(imgUrl);
            MyLog.e("yang", "setImageUrl===" + imgUrl);
        }

        oks.setUrl(shareUrl); //微信不绕过审核分享链接


        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(final Platform platform, int i, HashMap<String, Object> hashMap) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {

                        MyLog.e("yang", "platform==" + platform + "\n" + "onComplete");
                        if (platform.isClientValid()) {
                            MyLog.e("yang", "1111111111-----------" + platform.getName());
                            ShareEvent shareEvent = new ShareEvent();
                            shareEvent.setType("1");
                            shareEvent.setPlatform(getType(platform));
                            RxBus.getDefault().post(GOODS_SHARE_CODE, shareEvent);
                        }

                    }
                });

            }

            @Override
            public void onError(final Platform platform, int i, Throwable throwable) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        MyLog.e("yang", "platform==" + platform + "\n" + "onError");
                        if (platform.isClientValid()) {
                            MyLog.e("yang", "22222222------------");
                            ShareEvent shareEvent = new ShareEvent();
                            shareEvent.setType("2");
                            shareEvent.setPlatform(getType(platform));
                            RxBus.getDefault().post(shareEvent);
                        }

                    }
                });

            }

            @Override
            public void onCancel(final Platform platform, int i) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        MyLog.e("yang", "platform==" + platform + "\n" + "onCancel");
                        if (platform.isClientValid()) {
                            ShareEvent shareEvent = new ShareEvent();
                            shareEvent.setType("3");
                            shareEvent.setPlatform(getType(platform));
                            RxBus.getDefault().post(shareEvent);
                            MyLog.e("yang", "3333333-----");
                        }

                    }
                });

            }
        });


        // 构造一个图标
        Bitmap enableLogo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_copy);
        String label = "复制链接";
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                InitKeyBoardContext.init(context);
                ClipboardUtils.copyText(copy);
                PhoneUtils.ShowToastMessage(context, "链接复制成功");
            }
        };
        oks.setCustomerLogo(enableLogo, label, listener);
        // 启动分享
        oks.show(context);
    }

    public static String getType(Platform platform) {
        if ("QQ".equals(platform.getName())) {
            return "qq";
        }
        if ("QZone".equals(platform.getName())) {
            return "qqZone";
        }
        if ("SinaWeibo".equals(platform.getName())) {
            return "weibo";
        }
        if ("Wechat".equals(platform.getName())) {
            return "weChat";
        }
        if ("WechatMoments".equals(platform.getName())) {
            return "weChatCircle";
        }
        return "";
    }
}
